import java.util.PriorityQueue;

/**
 * @program: LeetCode
 * @ClassName Num040_getLeastNumbers
 * @description: LeetCode : 剑指 Offer 40. 最小的k个数
 * @author: WXY
 * @create: 2022-12-09 16:55
 * @Version 1.0
 **/


public class Num040_getLeastNumbers {
    public static int[] getLeastNumbers(int[] arr, int k) {
        int[] ans = new int[k];
        PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
        for (int i = 0; i < arr.length; i++) {
            heap.add(arr[i]);
        }
        int index = 0;
        while (k != 0) {
            ans[index++] = heap.poll();
            k--;
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {1, 5, 6, 3, 2, 4, 8, 9};
        int[] leastNumbers = getLeastNumbers(arr, 3);
        for (int l = 0; l < 2; l++) {
            System.out.print(leastNumbers[l] + " ");
        }
    }
}
